package com.example.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Efficiency;
import com.example.springboot.entity.Remember;
import com.example.springboot.mapper.EfficiencyMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/efficiency")
public class EfficiencyController {
    @Resource
    EfficiencyMapper efficiencyMapper;

    @PostMapping("/getxAxis")
    public Result<?> getxAxis(){
        QueryWrapper<Efficiency> wrapper = Wrappers.query();
        wrapper.last("limit 30");
        List<Efficiency> list = efficiencyMapper.selectList(wrapper);
        List<Integer> numberList = new ArrayList<>();
        int i = 0;
        for (Efficiency e : list) {
            ++i;
            numberList.add(i);
        }
        return Result.success(numberList);
    }
    @PostMapping("/getData")
    public Result<?> getData(){
        QueryWrapper<Efficiency> wrapper = Wrappers.query();
        wrapper.last("limit 30");
        List<Efficiency> list = efficiencyMapper.selectList(wrapper);
        List<Double> dataList = new ArrayList<>();
        int time = 0;
        int diffMinute = 0;
        double eff = 0;
        for (Efficiency e : list) {
            time = (int)(e.getEndTime().getTime() - e.getStartTime().getTime());
            diffMinute = time /1000/60;
            eff = e.getNumber()*1.0/diffMinute*30;
            dataList.add(eff);
        }
        return Result.success(dataList);
    }
    @GetMapping("/insert")
    public Result<?> insert(@RequestParam Integer number,
                            @RequestParam String startTime,
                            @RequestParam String endTime) throws ParseException {
        System.out.println(startTime);
        System.out.println(endTime);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Efficiency efficiency = new Efficiency();
        Date sTime = format.parse(startTime);
        Date eTime = format.parse(endTime);
        efficiency.setNumber(number);
        efficiency.setStartTime(sTime);
        efficiency.setEndTime(eTime);
        efficiencyMapper.insert(efficiency);
        return Result.success();
    }
}
